package com.hqq.concurrency.mutex;

/**
 * Mutex
 * 独占锁
 * Created by heqianqian on 2017/7/29.
 */
public class Mutex implements Lock {

    /**
     * 当前锁的拥有者
     */
    private Object owner;

    @Override
    public synchronized void acquire() throws InterruptedException {
        while (owner != null) {
            wait();
        }

        owner = Thread.currentThread();
    }

    @Override
    public synchronized void release() {
        if ( Thread.currentThread() == owner) {
            owner = null;
            notify();
        }
    }

    public Object getOwner() {
        return owner;
    }

    public void setOwner(Object owner) {
        this.owner = owner;
    }
}
